<?php

/**
 * BonEntre form base class.
 *
 * @method BonEntre getObject() Returns the current form's model object
 *
 * @package    SdCommerce
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseBonEntreForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                         => new sfWidgetFormInputHidden(),
      'reference'                  => new sfWidgetFormInputText(),
      'date_creation'              => new sfWidgetFormDate(),
      'date_validite'              => new sfWidgetFormDate(),
      'fournisseur_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Fournisseur'), 'add_empty' => true)),
      'departement_destination_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('DepartementDestination'), 'add_empty' => true)),
      'magasinie_id'               => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Employe'), 'add_empty' => true)),
      'observation'                => new sfWidgetFormTextarea(),
      'confirme'                   => new sfWidgetFormInputCheckbox(),
      'valider'                    => new sfWidgetFormInputCheckbox(),
      'etat_reglement'             => new sfWidgetFormInputCheckbox(),
      'livre'                      => new sfWidgetFormInputCheckbox(),
      'cloture'                    => new sfWidgetFormInputCheckbox(),
      'reference_bon_livraison'    => new sfWidgetFormInputText(),
      'created_at'                 => new sfWidgetFormDateTime(),
      'updated_at'                 => new sfWidgetFormDateTime(),
      'deleted_at'                 => new sfWidgetFormDateTime(),
      'm_stock_article_list'       => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'StockArticle')),
    ));

    $this->setValidators(array(
      'id'                         => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'reference'                  => new sfValidatorString(array('max_length' => 20)),
      'date_creation'              => new sfValidatorDate(),
      'date_validite'              => new sfValidatorDate(array('required' => false)),
      'fournisseur_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Fournisseur'), 'required' => false)),
      'departement_destination_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('DepartementDestination'), 'required' => false)),
      'magasinie_id'               => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Employe'), 'required' => false)),
      'observation'                => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'confirme'                   => new sfValidatorBoolean(array('required' => false)),
      'valider'                    => new sfValidatorBoolean(array('required' => false)),
      'etat_reglement'             => new sfValidatorBoolean(array('required' => false)),
      'livre'                      => new sfValidatorBoolean(),
      'cloture'                    => new sfValidatorBoolean(array('required' => false)),
      'reference_bon_livraison'    => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'created_at'                 => new sfValidatorDateTime(),
      'updated_at'                 => new sfValidatorDateTime(),
      'deleted_at'                 => new sfValidatorDateTime(array('required' => false)),
      'm_stock_article_list'       => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'StockArticle', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('bon_entre[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'BonEntre';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['m_stock_article_list']))
    {
      $this->setDefault('m_stock_article_list', $this->object->MStockArticle->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveMStockArticleList($con);

    parent::doSave($con);
  }

  public function saveMStockArticleList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['m_stock_article_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MStockArticle->getPrimaryKeys();
    $values = $this->getValue('m_stock_article_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MStockArticle', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MStockArticle', array_values($link));
    }
  }

}
